package cn.wolfcode.p2p.base.service.impl;

import java.util.Date;
import java.util.List;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.mapper.RealAuthMapper;
import cn.wolfcode.p2p.base.query.RealAuthQueryObject;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.event.RealAuthSuccessEvent;
import cn.wolfcode.p2p.util.Assert;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RealAuthServiceImpl implements IRealAuthService {

    @Autowired
    private RealAuthMapper realAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ApplicationContext context;
    @Value("${db.arya}")
    private String key;   //注入值

    /**
     * 申请实名认证
     *
     * @param realAuth
     */
    @Override
    public void apply(RealAuth realAuth) {
        //获取到申请人
        LoginInfo applier = UserContext.getLoginInfo();
        //获取基本资料对象
        UserInfo userInfo = userInfoService.getById(applier.getId());

        //有通过实名认证就不能再申请了
        Assert.isFalse(userInfo.hasRealAuth(), "亲您已经通过认证了");

        //判断有没申请记录
        Assert.isFalse(userInfo.getRealAuthId() != null, "亲,实名认证正在申请中,不能重复申请");

        //设置保存实名对象
        RealAuth auth = new RealAuth();
        auth.setRealName(realAuth.getRealName());
        auth.setSex(realAuth.getSex());
        auth.setBornDate(realAuth.getBornDate());
        auth.setIdNumber(realAuth.getIdNumber());
        auth.setAddress(realAuth.getAddress());
        auth.setImage1(realAuth.getImage1());
        auth.setImage2(realAuth.getImage2());
        auth.setApplyTime(new Date());
        auth.setApplier(applier);
        realAuthMapper.insert(auth,key);

        //设置userinfo中的realAuthId值
        userInfo.setRealAuthId(auth.getId());
        userInfoService.update(userInfo);
    }

    @Override
    public RealAuth getById(Long realAuthId) {
        return realAuthMapper.selectByPrimaryKey(realAuthId,key);
    }

    @Override
    public PageInfo listForPage(RealAuthQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List list = realAuthMapper.selectForPage(qo,key);
        return new PageInfo(list);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        //1.基本参数判断
        //2.判断当前实名对象状态为待审核
        RealAuth realAuth = realAuthMapper.selectByPrimaryKey(id,key);
        Assert.isFalse(realAuth.getState() != RealAuth.STATE_NORMAL, "当前实名状态不属于待审核状态");
        //3.设置审核相关信息
        //1.审核人
        realAuth.setAuditor(UserContext.getLoginInfo());
        //2.审核时间
        realAuth.setAuditTime(new Date());
        //4.审核备注
        realAuth.setRemark(remark);
        //5.统一设置审核状态
        realAuth.setState(state);
        //更新部分信息
        update(realAuth);

        //申请人的userInfo
        UserInfo userInfo = userInfoService.getById(realAuth.getApplier().getId());
        //4.如果审核成功：
        if (state == RealAuth.STATE_SUCCESS) {
            //1.修改申请人的userInfo.bitState添加实名位状态
            userInfo.addState(BitStatesUtils.OP_REAL_AUTH);
            //2.设置申请人的userInfo.realName
            userInfo.setRealName(realAuth.getRealName());
            //4.设置申请人的userInfo.idNumber
            userInfo.setIdNumber(realAuth.getIdNumber());

            //注入容器对象, 发布一个实名认证通过的消息,扩散到spring容器中
            this.context.publishEvent(new RealAuthSuccessEvent(this, realAuth));

        } else {
            //5.如果审核失败：
            //1.申请人的userInfo.realAuthId，设置为null
            userInfo.setRealAuthId(null);
        }
        userInfoService.update(userInfo);
    }

    @Override
    public void update(RealAuth realAuth) {
        realAuthMapper.updateByPrimaryKey(realAuth);
    }
}
